package com.fyf.game.net.business.server;

import org.je.util.StringUtil;

import com.fyf.game.net.Sender;
import com.fyf.game.net.business.Cmd;
import com.fyf.game.net.server.MsgSender;

/**
 * @author fanyifeng 2013-1-10
 */
public class LoginLogoutHandler
{
	/**
	 * 登录
	 * 
	 * @param id
	 * @param name
	 * @param pwd
	 * @param sender
	 */
	public void doLogin(String id, String name, String pwd, Sender sender) //
	{
		if (StringUtil.isBlankOrNull(id)) {
			sender.send(Cmd.login + Cmd.spr + Cmd.err + Cmd.spr + Cmd.id_is_empty);
			return;
		}

		if (sender.getID() != null) {
			sender.send(Cmd.login + Cmd.spr + Cmd.err + Cmd.spr + sender.getID());
			return;
		}

		Sender old = sender.getBox().getSender(id);

		if (old != null) {
			old.distroy();
		}

		MsgSender ms = (MsgSender)sender;
		ms.setID(id);
		ms.setName(name);
		sender.getBox().addSender(sender);

		for (Sender sd : sender.getBox().getAllSenders()) {
			sd.send(Cmd.login + Cmd.spr + Cmd.ok + Cmd.spr + id + Cmd.spr + name);
		}
	}

	/**
	 * 登出
	 * 
	 * @param sender
	 */
	public void doLogout(Sender sender) {
		for (Sender sd : sender.getBox().getAllSenders()) {
			sd.send(Cmd.logout + Cmd.spr + Cmd.ok + Cmd.spr + sender.getID() + Cmd.spr
					+ sender.getName());
		}
		sender.getBox().remove(sender.getID());
	}
}
